package com.gyx.lottery.service.impl;

import com.gyx.lottery.controller.param.PriCreParam;
import com.gyx.lottery.controller.param.PageParam;
import com.gyx.lottery.dao.dataobject.PrizeDO;
import com.gyx.lottery.dao.mapper.PrizeMapper;
import com.gyx.lottery.service.PictureService;
import com.gyx.lottery.service.PrizeService;
import com.gyx.lottery.service.dto.PageListDTO;
import com.gyx.lottery.service.dto.PrizeDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;
import java.util.stream.Collectors;

@Service

public class PrizeServiceImpl implements PrizeService {
    @Autowired
    PictureService pictureService;
    @Autowired
    PrizeMapper prizeMapper;
    @Override
    public Long createPrize(PriCreParam param, MultipartFile prizePic) {
        PrizeDO prizeDO=new PrizeDO();
        prizeDO.setName(param.getPrizeName());
        prizeDO.setDescription(param.getDescription());
        prizeDO.setPrice(param.getPrice());
        String fileName = pictureService.savePicture(prizePic);
        prizeDO.setImageUrl(fileName);
        prizeMapper.insert(prizeDO);
        return  prizeDO.getId();
    }

    @Override
    public PageListDTO<PrizeDTO> findPrizeList(PageParam param) {
        //获取总数
        int count=prizeMapper.count();
        int offset=param.getOffset();
        int pageSize= param.getPageSize();
        List<PrizeDO>prizeDOList=prizeMapper.selectPrizeListByPage(offset,pageSize);
        //获取列表

        List<PrizeDTO> prizeDTOList = prizeDOList.stream().map(prizeDO -> {
            PrizeDTO prizeDTO = new PrizeDTO();
            prizeDTO.setPrizeId(prizeDO.getId());
            prizeDTO.setPrizeName(prizeDO.getName());
            prizeDTO.setPrice(prizeDO.getPrice());
            prizeDTO.setDescription(prizeDO.getDescription());
            prizeDTO.setImageUrl(prizeDO.getImageUrl());
            return prizeDTO;
        }).collect(Collectors.toList());
        PageListDTO<PrizeDTO>prizeDTOPageListDTO=new PageListDTO<>(count,prizeDTOList);
        return  prizeDTOPageListDTO;
    }
}
